define(
	[
		'require'
	],
	function(require){

		function actionFormatter(value, row, index) {
	        return '<div class="form-column-summary" style="text-align:left;width:100%;" title="'+value+'">'+APM.util.escape(value)+'</div>';
	    }
	    var numData = [];
		var Controller = Ember.Controller.extend({
	
			startDate:Ember.computed(function(){
				var date = new Date(new Date() - timeD_value - 3600000*24-60000);
				date.setHours(0);
				date.setMinutes(0);
				date.setSeconds(0);
				date.setMilliseconds(0);
				date.setDate(date.getDate()+1);
				return new Date(+date);
			}),
			endDate:Ember.computed(function(){
				var date = new Date(new Date() - timeD_value-60000);
				date.setHours(0);
				date.setMinutes(0);
				date.setSeconds(0);
				date.setMilliseconds(0);
				date.setDate(date.getDate()+1);
				return new Date(+date);
			}),
			formTitle: "关键交易性能报表",

			businessData: ["count"],
			responseTimeData: ["responseTime"],
			successRateData: ["success_rate"],
			
			columns: [
			  [  {
			    "field": "channelName",
			    "title": "渠道",
			    formatter:actionFormatter,
			    width:"10%",
			    "colspan": 1,
			    "rowspan": 2,
			    // "sortable": true

			  }
			  , {
			    "title": "交易量",
			    "colspan": 2,
			    width:"18%",
			    "rowspan": 1
			  },
			  {
			    "title": "平均响应时间(ms)",
			    "colspan": 2,
			    "rowspan": 1
			  },
			  {
			    "title": "成功率%",
			    "colspan": 2,
			    "rowspan": 1
			  },
			  {
			    "title": "错误数",
			    "colspan": 2,
			    "rowspan": 1
			  }], 
			  [{
			    "field": "count",
			    "title": "本期",
			    "colspan": 1,
			    "rowspan": 1,
			    // "sortable": true
			  }, {
			    "field": "business/throughput/sequential_rate",
			    "title": "环比%",
			    "colspan": 1,
			    "rowspan": 1,
			  }, {
			    "field": "responseTime",
			    "title": "本期",
			    "colspan": 1,
			    "rowspan": 1,

			  }, {
			    "field": "business/response_time/sequential_rate",
			    "title": "环比%",
			    "colspan": 1,
			    "rowspan": 1,
			  }, {
			    "field": "success_rate",
			    "title": "本期",
			    "colspan": 1,
			    "rowspan": 1,

			  }, {
			    "field": "business/success/sequential_rate",
			    "title": "环比%",
			    "colspan": 1,
			    "rowspan": 1,
			  }, {
			    "field": "isError",
			    "title": "本期",
			    "colspan": 1,
			    "rowspan": 1,

			  }, {
			    "field": "business/error_count/sequential_rate",
			    "title": "环比%",
			    "colspan": 1,
			    "rowspan": 1,
			  }]
			],
			//根据数据来填充表格里的内容
			fillTableContent: function(node,index){
				$(".table-left-container .tbody").append("<div class='table-left-tr clearfix'></div>");
				var tableTr = $(".table-left-container .tbody .table-left-tr").eq(index);
				var num = ((node.channel.length+1)*50);
				if(index == 0){
					num = num - 1;
				}
				$(".table-left-container .tbody .table-left-tr").eq(index).css({"height":num+"px","line-height":num+"px"});
				if(node.typeName){
					$("<div class='table-left-td'><div class='word-break'>"+node.typeName+"</div></div>").appendTo(tableTr);
				}else{
					$("<div class='table-left-td'>-</div>").appendTo(tableTr);
				}

				if(node.serviceName){
					$("<div class='table-left-td'><div class='word-break'>"+node.serviceName+"</div></div>").appendTo(tableTr);
				}else{
					$("<div class='table-left-td'>-</div>").appendTo(tableTr);
				}
				$(".table-left-container .tbody .table-left-tr").eq(index).find(".word-break").css("marginTop",(num-$(".word-break").eq(index).height())/2);
			},
			//根据数据插入表格头部
			fillTableHeader: function(){
				$(".table-left-container .thead").find("div").remove();
				$(".table-left-container .thead").append("<div class='clearfix table-left-tr'></div>");
				var tableTr = $(".table-left-container .thead .table-left-tr");
				$("<div class='table-left-td pull-left'>交易名称</div>").appendTo(tableTr);
				$("<div class='table-left-td pull-left'>服务节点</div>").appendTo(tableTr);
			},
			applicationValueChange: Ember.observer("applicationValue",function(){
				this.reportformData();
				
			}),
			/*
				向服务端请求业务系统列表
			*/
			requireApplications: function(){
				var ctrl = this;
				return this.store.ajax("GET", "applications")
				.then(function(response){
					var applications = Ember.A(response.applications);
					ctrl.set("applications",applications);
					ctrl.set("applicationValue",applications[0].id);
					return applications;
				});
			},
			
			reportformData: function(){

				if(!this.get("applicationValue")){
					return;
				}
		
				var ctrl = this;
				var params = {
					"startDate": +this.get("startDate"),
					"endDate": +this.get("endDate"),
					"appId": ctrl.get("applicationValue"),
				}
				var newArray = [],
					newObject,
					echartsDate = [];

				ctrl.set("tipTitle","");
				numData = [];
				return this.store.ajax("GET", "report/trade/perf",params)
				.then(function(response){
					var performances = response.performances;
					if(performances[0]){
						ctrl.fillTableHeader();
						$(".table-left-container .tbody").find("div").remove();
						performances.forEach(function(node,index){
							
							ctrl.fillTableContent(node,index);
							numData.push(node["channel"].length+1);
							newArray.push(node);
							newObject = Ember.merge(node,{"channelName":"合计"});
							node.channel.forEach(function(app){
								newArray.push(Ember.merge(app,{"typeName":node.typeName}));
							});
						});
					}else{
						$(".table-left-container .tbody").find("div").remove();
						$(".table-left-container .tbody").append("<div class='table-left-tr clearfix'></div>");
						$(".table-left-container .tbody .table-left-tr").css("height","49px");
					
						ctrl.fillTableHeader();
						ctrl.set("tipTitle","这段时间内暂无数据");
					}
					ctrl.set("groupData",newArray);
					Ember.A(newArray).forEach(function(node,index){
						if(node["channelName"] == '合计'){
							node["typeName"] = node["typeName"] +"@"+ node["serviceName"];
							echartsDate.push(node);
						}
					});
					ctrl.set("echartsDate",echartsDate);
					var j = 0;
					var clear = setTimeout(function(){
						numData.forEach(function(node,index){
							if(index%2 == 0){
								for(var i=0;i<node;i++){

									$(".fixed-table-container tbody tr").eq(j+i).find("td").css("background"," rgba(0,0,0,.25)");
									
								}
							
							}else{
								for(var i=0;i<node;i++){
									$(".fixed-table-container tbody tr").eq(j+i).find("td").css("background"," rgba(0,0,0,.12)");
								}
							}
							j = j+node;
						});
						var width = $(".table-wrapper").width();
						$(".fixed-table-container tbody .no-records-found td").css({"textAlign":"left"}).css({"padding-left":(width/2-(width*0.2))-24 + "px"});
					},0);
					ctrl.set("clearTime",clear);
				});
			},
			url:"report/trade/perf/download",
			actions: {
				timeRangesChange: function(timeRange){
					this.set("startDate",timeRange.startDate);
					this.set("endDate",timeRange.endDate);

					this.reportformData();
				},
				export: function(){
					var ctrl = this,
						appId = ctrl.get("applicationValue"),
						startDate = +ctrl.get("startDate"),
						endDate = +ctrl.get("endDate"),
						top = ctrl.get("formTopValue");
						
					confirmDialog("确定要导出报表吗？")
					.then(function(){
						var url="/front/rest/apm/"+ctrl.get("url")+"?top="+top+"&appId="+appId+"&startDate="+startDate+"&endDate="+endDate+"&token="+encodeURIComponent($.cookie("token"));
						window.location.href =url;
					});
				}
			}
		});

		APM.FormPerformanceController = Controller;
		return Controller;
	}
);